package com.company.project.service.impl;

import com.company.project.dao.AuthorsMapper;
import com.company.project.dao.ThesisDeliveryMapper;
import com.company.project.model.Thesis;
import com.company.project.model.ThesisDelivery;
import com.company.project.service.AdviserService;
import com.company.project.service.AuthorsService;
import com.company.project.service.ThesisDeliveryService;
import com.company.project.core.AbstractService;
import com.company.project.service.ThesisService;
import com.company.project.util.ThesisDeliveryAndAuthors;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class ThesisDeliveryServiceImpl extends AbstractService<ThesisDelivery> implements ThesisDeliveryService {
    @Resource
    private ThesisDeliveryMapper thesisDeliveryMapper;

    @Resource
    private AuthorsService authorsService;

    @Resource
    private ThesisService thesisService;

    @Override
    public Integer selectNew() {
        return thesisDeliveryMapper.selectNew();
    }

    @Override
    public Boolean insertThesisDeliveryAndAuthor(ThesisDeliveryAndAuthors thesisDeliveryAndAuthors) {
        int k = 0;
        Date date = new Date();
        Thesis thesis = new Thesis();
        if(thesisDeliveryAndAuthors.getThesisDelivery().getTid()==null || ("").equals(thesisDeliveryAndAuthors.getThesisDelivery().getTid())){
            //直投添加
            thesis=thesisDeliveryAndAuthors.getThesis();
            thesis.setCreateTime(date);
            thesis.setLastModifyTime(date);
            k=thesisService.insertSelective(thesis);
            if(k!=0){
                thesisDeliveryAndAuthors.getThesisDelivery().setTid(thesis.getId());
            }
        }else{
            //非直投修改
            thesisDeliveryAndAuthors.getThesis().setId(thesisDeliveryAndAuthors.getThesisDelivery().getTid());
            thesisDeliveryAndAuthors.getThesis().setLastModifyTime(new Date());
            k = thesisService.updateParticularsById(thesisDeliveryAndAuthors.getThesis());
        }
        thesisDeliveryAndAuthors.getThesisDelivery().setCreateTime(date);
        thesisDeliveryAndAuthors.getThesisDelivery().setLastModifyTime(date);
        thesisDeliveryAndAuthors.getThesisDelivery().setDeliverytime(date);
        thesisDeliveryAndAuthors.getThesisDelivery().setState("0");
        Integer n = thesisDeliveryMapper.insert(thesisDeliveryAndAuthors.getThesisDelivery());
        int m = 0;
        for (int i = 0; i < thesisDeliveryAndAuthors.getAuthors().size(); i++) {
            thesisDeliveryAndAuthors.getAuthors().get(i).setTid(thesisDeliveryAndAuthors.getThesisDelivery().getTid());
            thesisDeliveryAndAuthors.getAuthors().get(i).setCreateTime(date);
            thesisDeliveryAndAuthors.getAuthors().get(i).setLastModifyTime(date);
            m += authorsService.InsetsAuthorsList(thesisDeliveryAndAuthors.getAuthors());
        }
        if ((n == 1) && (m == thesisDeliveryAndAuthors.getAuthors().size()) && (k==1)) {
            return true;
        }
        return false;
    }

    /**
     * 前台稿件直投
     * @param id
     * @return
     */
    @Override
    public List<Map<Object,Object>> selectTD(Integer id) {
        return thesisDeliveryMapper.selectTD(id);
    }
}
